package com.xl.rentkeeper.act;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.xl.rentkeeper.App;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.view.ClipImageLayout;
import com.xl.rentkeeper.view.ImageTools;

import java.io.File;

public class ClipActivity extends Activity{
	private ClipImageLayout mClipImageLayout;
	private String path;
	private ProgressDialog loadingDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState){
		requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_clipimage);
		//这步必须要加
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //设置加载dialog,作为加载数据的提示
		loadingDialog=new ProgressDialog(this);
        loadingDialog.setTitle("请稍后...");

		path=getIntent().getStringExtra("path");
		if(TextUtils.isEmpty(path)||!(new File(path).exists())){
			App.showToast("图片加载失败");
			return;
		}
		Bitmap bitmap= ImageTools.convertToBitmap(path, 600, 600);
		if(bitmap==null){
			App.showToast("图片加载失败");
			return;
		}
		mClipImageLayout = (ClipImageLayout) findViewById(R.id.id_clipImageLayout);
		mClipImageLayout.setBitmap(bitmap);

		((Button)findViewById(R.id.id_action_clip)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				loadingDialog.show();
				new Thread(new Runnable() {
					@Override
					public void run() {
						Bitmap bitmap = mClipImageLayout.clip();
						String saveName = System.currentTimeMillis()+ ".png";
						String path= Environment.getExternalStorageDirectory()+ClipImageLayout.savePath+"/cache/"+saveName;

						// 获取SharedPreferences对象
						SharedPreferences sp = getSharedPreferences("xiaoluo", Activity.MODE_PRIVATE);
						// 获取Editor对象
						SharedPreferences.Editor editor = sp.edit();
						editor.putString("saveName",saveName);
						editor.commit();

						ImageTools.savePhotoToSDCard(bitmap,path);
						loadingDialog.dismiss();
						Intent intent = new Intent();
						intent.putExtra("path",path);
						setResult(RESULT_OK, intent);
						finish();
					}
				}).start();
			}
		});
	}



}
